package com.yaolaizaixian.merchant.home;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yaolaizaixian.merchant.HomeActivity;
import com.yaolaizaixian.merchant.LoginActivity;
import com.yaolaizaixian.merchant.R;
import com.yaolaizaixian.merchant.order.OrderListActivity;
import com.yaolaizaixian.merchant.utils.SharedPreferencesUtil;
import com.zhy.autolayout.AutoRelativeLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.drakeet.materialdialog.MaterialDialog;

/**
 * Created by Administrator on 2017/2/23.
 */
public class Fragment_mune extends Fragment {

    private String TAG="Fragment_mune";
    @BindView(R.id.tv_head)
    ImageView tvHead;
    @BindView(R.id.tv_shopname)
    TextView tvShopname;
    @BindView(R.id.tv_username)
    TextView tvUsername;
    @BindView(R.id.iv_order)
    ImageView ivOrder;
    @BindView(R.id.rl_order)
    AutoRelativeLayout rlOrder;
    @BindView(R.id.iv_sale)
    ImageView ivSale;
    @BindView(R.id.rl_sale)
    AutoRelativeLayout rlSale;
    @BindView(R.id.iv_stting)
    ImageView ivStting;
    @BindView(R.id.rl_stting)
    AutoRelativeLayout rlStting;
    @BindView(R.id.iv_logout)
    ImageView ivLogout;
    @BindView(R.id.rl_logout)
    AutoRelativeLayout rlLogout;
    @BindView(R.id.ll_leftmune)
    AutoRelativeLayout llLeftmune;
    private HomeActivity homeActivity;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_left_menu, container, false);
        ButterKnife.bind(this, view);
        homeActivity = (HomeActivity) getActivity();

        String logoimage = SharedPreferencesUtil.getString(getActivity(), "shopImage", "");


        Log.e(TAG, "logoimage==" + logoimage);

        //用户名称
        String username = SharedPreferencesUtil.getString(getActivity(), "username", "");
        String Shopname = SharedPreferencesUtil.getString(getActivity(), "Shopname", "");
        //员工部门
        String menname=SharedPreferencesUtil.getString(getActivity(), "menname","");


        tvShopname.setText("【"+Shopname+"】");
        if (!TextUtils.isEmpty(menname)){

            tvUsername.setText(username+"("+menname+")");

        }else {

            tvUsername.setText(username);
        }


        Glide
                .with(getActivity())
                .load(logoimage)
                .placeholder(R.mipmap.noload)
                .error(R.mipmap.noload)
                .into(tvHead);

        return view;
    }

    @OnClick({R.id.rl_order, R.id.rl_sale, R.id.rl_stting, R.id.rl_logout})
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.rl_order:

                startActivity(new Intent(getActivity(), OrderListActivity.class));

                break;

            case R.id.rl_sale:

                startActivity(new Intent(getActivity(), SalesRecordActivity.class));

                break;

            case R.id.rl_stting:

                startActivity(new Intent(getActivity(), SttingActivity.class));

                break;
            case R.id.rl_logout:


                final MaterialDialog mMaterialDialog = new MaterialDialog(getActivity());
                mMaterialDialog.setMessage("是否退出当前账号？");
                mMaterialDialog.setPositiveButton("确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        //员工id
                        SharedPreferencesUtil.saveString(getActivity().getApplicationContext(), "usersysno","");

                        startActivity(new Intent(getActivity(), LoginActivity.class));
                        getActivity().finish();
                    }
                });

                mMaterialDialog.setNegativeButton("取消", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        mMaterialDialog.dismiss();
                    }
                });

                mMaterialDialog.show();

                break;
        }
    }
public void changeImage(){

    String logoimage = SharedPreferencesUtil.getString(getActivity(), "shopImage", "");

    Log.e(TAG, "logoimage==" + logoimage);

    //用户名称
    String username = SharedPreferencesUtil.getString(getActivity(), "username", "");
    String Shopname = SharedPreferencesUtil.getString(getActivity(), "Shopname", "");
    //员工部门
    String menname=SharedPreferencesUtil.getString(getActivity(), "menname","");

    tvShopname.setText("[" + Shopname + "]");
    if (TextUtils.isEmpty(menname)){
        tvUsername.setText(username);
    }else {
        tvUsername.setText(username + "(" + menname + ")");
    }

    Glide
            .with(getActivity())
            .load(logoimage)
            .placeholder(R.mipmap.noload)
            .error(R.mipmap.noload)
            .into(tvHead);

}

}
